import * as mongoose from 'mongoose';

import fastFormatTimeSchema from './base';

const { Schema } = mongoose;

const fileSchema = new Schema(
  {
    fileName: Schema.Types.String,
    md5: Schema.Types.String,
    size: Schema.Types.Number,
    mime: Schema.Types.String,
    storagePath: Schema.Types.String,
    uploader: {
      type: Schema.Types.ObjectId,
      ref: 'user',
    },
    ...fastFormatTimeSchema(),
  },
  {
    versionKey: false,
    timestamps: true,
    toJSON: {
      getters: true,
      transform(doc, ret) {
        ret.id = ret._id;
        delete ret._id;
      },
    },
    toObject: {
      getters: true,
      transform(doc, ret) {
        ret.id = ret._id;
        delete ret._id;
      },
    },
  },
);

export default fileSchema;
